﻿using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.UI;

// 游戏管理类
public class GameManager : MonoBehaviour {

    // 常量.
    public const int MachingCount = 3;
    // 选择中拼图的颜色
    private const float SelectedPieceAlpha = 0.5f;

    // 选择中的拼图对象
    private GameObject selectedPieceObject;

    // 枚举.
    private enum GameState {
        // 等待输入
        Idle,
        // 移动拼图
        PieceMove,
        // 检查匹配
        MatchCheck,
        // 删除匹配
        DeletePiece,
        // 填充新拼图
        FillPiece,

        Wait,
        // 等待
    }

    // 使用对象.
    [SerializeField]
    private Board board;

    // 当前状态
    private GameState currentState;

    // 正在选择中的拼图
    private Piece selectedPiece;

    [SerializeField]
    private UIManager uiManager;

    //-------------------------------------------------------
    // MonoBehaviour Function
    //-------------------------------------------------------
    // 游戏的初始化处理
    private void Start ( ) {
        board.InitializeBoard (6, 5);

        currentState = GameState.Idle;
    }

    // 游戏的主流程
    private void Update ( ) {
        switch (currentState) {
        case GameState.Idle:
            Idle ( );
            break;
        case GameState.PieceMove:
            PieceMove ( );
            break;
        case GameState.MatchCheck:
            MatchCheck ( );
            break;
        case GameState.DeletePiece:
            DeletePiece ( );
            break;
        case GameState.FillPiece:
            FillPiece ( );
            break;
        default:
            break;
        }

        uiManager.SetStatusText ("当前状态:" + currentState.ToString ( ));
    }

    //-------------------------------------------------------
    // Private Function
    //-------------------------------------------------------
    // 检查玩家是否有选择拼图、选择拼图之后，让系统进入选择状态
    private void Idle ( ) {
        if (Input.GetMouseButtonDown (0)) {
            // // 寻找出玩家正在选择的拼图
            // selectedPiece = board.GetStartPiece (Input.mousePosition);

            // // 使其半透明
            // selectedPiece.SetPieceAlpha (SelectedPieceAlpha);

            // // 如果玩家选择到了拼图 进入拼图移动状态
            // if (selectedPiece != null) {
            //     // 状态迁移
            //     currentState = GameState.PieceMove;
            // }

            // 还原Combo
            uiManager.ResetCombo ( );
            SelectPiece ( );
        }
    }

    //　选择拼图的处理
    private void SelectPiece ( ) {
        // selectedPiece = board.GetNearestPiece (Input.mousePosition);

        selectedPiece = board.GetStartPiece (Input.mousePosition);

        if (selectedPiece == null) {
            return;
        }

        // 生成一个大一号的被选择中的拼图
        var piece = board.InstantiatePiece (Input.mousePosition);
        piece.SetKind (selectedPiece.GetKind ( ));
        piece.SetSize ((int) (board.pieceWidth * 1.2f));
        piece.SetPieceAlpha (SelectedPieceAlpha);
        selectedPieceObject = piece.gameObject;

        selectedPiece.SetPieceAlpha (SelectedPieceAlpha);
        currentState = GameState.PieceMove;
    }

    // 玩家选择了拼图时的处理、如果检查到输入结束之后，系统进入检查棋盘状态
    private void PieceMove ( ) {
        if (Input.GetMouseButton (0)) {
            // 输入还未结束
            // 检查距离点击最近的一个拼图
            var piece = board.GetNearestPiece (Input.mousePosition);
            // 如果不是同一个拼图
            if (piece != selectedPiece) {
                // 选择中的拼图和拼图交换位置
                board.SwitchPiece (selectedPiece, piece);
            }
            // 靠上一点 显示被选择的拼图
            selectedPieceObject.transform.position = Input.mousePosition + Vector3.up * 10;
        } else if (Input.GetMouseButtonUp (0)) {

            // 恢复其颜色
            selectedPiece.SetPieceAlpha (1f);
            // 销毁选择中的拼图
            Destroy (selectedPieceObject);
            // 输入结束
            currentState = GameState.MatchCheck;
        }
    }

    // 检查棋盘上是否有匹配的拼图
    private void MatchCheck ( ) {
        if (board.HasMatch ( )) {
            // 如果有匹配拼图进入删除
            currentState = GameState.DeletePiece;
        } else {
            // 回到初始状态
            currentState = GameState.Idle;
        }
    }

    // 删除已被匹配的拼图
    private void DeletePiece ( ) {

        // 进入等待
        currentState = GameState.Wait;
        // 迁移至填充棋盘
        StartCoroutine (board.DeleteMatchPiece (( ) => currentState = GameState.FillPiece));

        // board.DeleteMatchPiece ( );
        // 迁移至填充棋盘
        // currentState = GameState.FillPiece;
    }

    // 补充盘面上被删除的拼图
    private void FillPiece ( ) {

        // 进入等待
        currentState = GameState.Wait;
        // 回调迁移至配对检查
        StartCoroutine (board.FillPiece (( ) => currentState = GameState.MatchCheck));

        // board.FillPiece ( );
        // 迁移至配对检查
        // currentState = GameState.MatchCheck;
    }
}
